$(function () {
    $("#jqGrid").jqGrid({
        url: '/platform/oem/cardRaiderList',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'raiderId', index: 'raiderId', width: 50 ,key:true,hidden:true},
            { label: '标题', name: 'title', index: 'title'},
            { label: 'icon', name: 'image', index: 'image',formatter:function (value) {
                return '<img src="'+value+'" width="30" height="30" >';
            }},
            { label: '链接地址', name: 'link', index: 'link'}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "result.list",
            page: "result.currPage",
            total: "result.totalPage",
            records: "result.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条\
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    $("#raiderImage").click(function () {
        $("#raiderInput").click();
    });
    $("#raiderInput").change(function () {
        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
        if (objUrl != null) {
            $("#raiderImage").attr("src", objUrl);
        }
    });

});

Vue.prototype.brs = function (value) {
    return value == null?null:value.replace(/\##/g,"<br>");
};

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		isUpdate:false,
		raider: {},
	},
	methods: {
		query: function () {
			vm.reload();
		},
        add:function () {
            vm.title = "添加攻略";
            vm.showList = false;
        },
        del:function () {
		    var id = getSelectedRow();
		    if (id == null)
		        return;
		    window.confirm("确定要删除这一条信息么？",function () {
                $.getJSON("/platform/oem/delCardRaider/"+id,null,function (r) {
                    if (r.code == 200){
                        alert("操作成功");
                        vm.reload();
                    }else
                        alert(r.message);
                });
            });
        },
        saveOrUpdate:function () {
		    var urlStr = "/platform/oem/addCardRaider";
            $.ajaxFileUpload({
                url: urlStr,
                data: JSON.parse(JSON.stringify(vm.raider)),
                type: "POST",
                contentType: "multipart/form-data",
                secureuri: false,
                fileElementId: 'raiderInput',
                dataType: 'json',
                success: function (result) {
                    if (result.code == 200) {
                        vm.reload();
                        alert("操作成功");
                    } else {
                        alert(result.message);
                        if (this.lecture.id == null) {
                            $("#loanIconImage").attr("src", "");
                        }
                    }
                },
                complete: function () {
                    $("#raiderInput").replaceWith('<input id="raiderInput" name="icon" accept="image/*" type="file" style="display: none;" class="form-control"/>');
                    $("#raiderInput").change(function () {
                        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
                        if (objUrl != null) {
                            $("#raiderImage").attr("src", objUrl);
                        }
                    });
                },
                error: function (e) {
                    console.log("error", e);
                    $("#raiderImage").attr("src", "");
                    alert("请求失败，请重试");
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            vm.showInfo = false;
			$("#jqGrid").jqGrid('setGridParam',{
                page:1
            }).trigger("reloadGrid");
            vm.isUpdate = false;
		}
	}
});